package com.xbongbong.pro.statistic.service.result;

import com.xbongbong.i18n.util.I18nMessageUtil;
import com.xbongbong.paas.enums.IndexTypeEnum;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.util.BeanUtil;
import com.xbongbong.paas.toolbox.util.DateUtil;
import com.xbongbong.pro.constant.CommonConstant;
import com.xbongbong.pro.service.toolbox.statistic.util.ChartResultUtil;
import com.xbongbong.pro.statistic.constant.ChartI18Constant;
import com.xbongbong.pro.statistic.enums.NumFormatEnum;
import com.xbongbong.pro.statistic.enums.SystemCodeEnum;
import com.xbongbong.pro.statistic.enums.ValueTypeEnum;
import com.xbongbong.pro.statistic.result.pojo.ChartResultPojo;
import com.xbongbong.pro.statistic.result.pojo.ElasticAggsPojo;
import com.xbongbong.pro.statistic.result.pojo.PanoramaPojo;
import com.xbongbong.pro.statistic.result.pojo.dto.StatisticResultDTO;
import com.xbongbong.pro.statistic.result.pojo.dto.StatisticResultInfoDTO;
import com.xbongbong.pro.statistic.result.pojo.vo.ChartResultDetailVO;
import com.xbongbong.saas.enums.business.InvoiceEnum;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.springframework.stereotype.Component;

import java.util.Arrays;
import java.util.List;

/**
 * 发票统计
 * @author zcp
 * @version v1.0
 * @date 2019/3/21 22:03
 * @since v1.0
 */
@Component
public class CrmInvoiceResult extends ChartResultParentService implements ChartResultSystem {

    @Override
    public Object getTarget(ChartResultPojo chartResultPojo, ChartResultDetailVO chartResultDetailVO) throws XbbException {
        SystemCodeEnum systemCodeEnum = chartResultPojo.getSystemCodeEnum();
        switch (systemCodeEnum) {
            case INVOICE_ED_CHART:
                return invoiceEdChart(chartResultPojo, chartResultDetailVO);
            default:
                break;
        }
        return null;
    }

    @Override
    public Object getValueTarget(ChartResultPojo chartResultPojo) throws XbbException {
        SystemCodeEnum systemCodeEnum = chartResultPojo.getSystemCodeEnum();
        switch (systemCodeEnum) {
            //销项开票（资金管理）
            case INVOICE_ED_CHART:
            case INVOICE_HOME:
                return invoiceEdBoard(chartResultPojo);
            default:
                break;
        }
        return null;
    }

    /**
     * 发票统计
     * @param chartResultPojo
     * @param chartResultDetailVO
     * @return java.lang.Object
     * @throws XbbException
     * @author zcp
     * @date 2019/3/22 15:40
     * @since v1.0
     * @version v1.0
     */
    public Object invoiceEdChart(ChartResultPojo chartResultPojo, ChartResultDetailVO chartResultDetailVO) throws XbbException {
        List<String> legendList = Arrays.asList(I18nMessageUtil.getMessage(ChartI18Constant.INVOICE_CHART_TITLE).split(","));
        String unit = systemChartHelp.getUnit(I18nMessageUtil.getMessage(CommonConstant.YUAN));
        String unit1 = systemChartHelp.getUnit("%");
        List<String> unitList = Arrays.asList(unit, unit, unit, unit1, unit1);
        StatisticResultDTO statisticResultDto = getStatisticResultDto4MomYoy(chartResultPojo, legendList, unitList);

        //右侧展示总数
        statisticResultDto.setTotalFlag(true);
        statisticResultDto.setValueTitle(I18nMessageUtil.getMessage(ChartI18Constant.TOTAL_INVOICED_AMOUNT));
        chartResultDetailVO = systemChartHelp.packageResult(statisticResultDto, chartResultPojo, chartResultDetailVO);
        return chartResultDetailVO;
    }

    /**
     * 开票总金额
     * @param chartResultPojo
     * @return java.lang.String 
     * @throws XbbException
     * @author zcp
     * @date 2019/4/15 21:49
     * @since v1.0
     * @version v1.0
     */
    private String invoiceEdBoard(ChartResultPojo chartResultPojo) throws XbbException {
        BoolQueryBuilder boolQueryBuilder = chartQueryHelp.invoiceQueryBuilder(chartResultPojo);
        String sumField = InvoiceEnum.getEsAttr4Keyword(InvoiceEnum.TOTAL_MONEY);
        double amount = systemChartHelp.getDouble4Total(IndexTypeEnum.IDX_SAAS_INVOICE, boolQueryBuilder, sumField);
        return ChartResultUtil.formatValue2Double(false, ValueTypeEnum.VALUE_MONEY, false, amount, NumFormatEnum.getByCode(chartResultPojo.getNumFormat()), chartResultPojo.getNumFormatFlag());
    }

    /**
     * 全景图发票金额
     *
     * @param panoramaPojo 全景图入参
     * @param isCancel 是否作废
     * @param isRed 是否红冲
     * @return java.lang.String
     * @throws XbbException 异常
     * @author 徐俊杰
     * @date 2019/11/27 15:51
     * @since v1.0
     */
    String panoramaCustomerInvoiceAmount(PanoramaPojo panoramaPojo, Integer isCancel, Integer isRed) throws XbbException {
        BoolQueryBuilder invoiceQuery = chartQueryHelp.panoramaQuery4Invoice(panoramaPojo, isCancel, isRed);
        String sumField = InvoiceEnum.getEsAttr4Keyword(InvoiceEnum.TOTAL_MONEY);
        double value = systemChartHelp.getDouble4Total(IndexTypeEnum.IDX_SAAS_INVOICE, invoiceQuery, sumField);
        return ChartResultUtil.formatValue2Double(false, ValueTypeEnum.VALUE_MONEY, false, value, NumFormatEnum.DEFAULT, false);
    }

    /**
     * 获取并封装环比、同比统计结果
     * @param chartResultPojo 参数pojo
     * @param legendList 图的legend
     * @param unitList 各条线的单位，与legendList大小一致
     * @return com.xbongbong.pro.statistic.pojo.dto.StatisticResultDTO
     * @throws XbbException
     * @author zcp
     * @date 2019/3/22 15:34
     * @since v1.0
     * @version v1.0
     */
    private StatisticResultDTO getStatisticResultDto4MomYoy(ChartResultPojo chartResultPojo, List<String> legendList, List<String> unitList) throws XbbException {
        Integer startTime = chartResultPojo.getStartTime();
        Integer endTime = chartResultPojo.getEndTime();
        //环比，上月开始--截止，startTime的前一个月，作为环比的开始时间；endTime的前一个月，作为环比的截止时间
        Integer lastMonthStartTime = DateUtil.getFirstDateOfLastMonth(startTime);
        Integer lastMonthEndTime = DateUtil.getFirstDateOfLastMonth(endTime);
        //同比，上年同期（上年同一个月）开始--截止：startTime的上年同月，作为同比的开始时间；endTime的上年同月第一天，作为同比的截止时间
        Integer lastYearStartTime = DateUtil.getFirstDateOfLastYear(startTime);
        Integer lastYearEndTime = DateUtil.getFirstDateOfLastYear(endTime);

        //------当前筛选时间的结果
        ElasticAggsPojo thisMonthAggsPojo = invoiceEd2ElasticAggsPojo(chartResultPojo, ValueTypeEnum.ARRAY_TYPE);

        //先把chartResultPojo拷贝出一份，下面都用chartResultPojoCopy，防止时间的变更对原有chartResultPojo内参数的影响
        ChartResultPojo chartResultPojoCopy = new ChartResultPojo();
        BeanUtil.copyProperties(chartResultPojo, chartResultPojoCopy);
        //------环比结果，把开始结束时间置为环比的开始结束
        chartResultPojoCopy.setStartTime(lastMonthStartTime);
        chartResultPojoCopy.setEndTime(lastMonthEndTime);
        ElasticAggsPojo lastMonthAggsPojo = invoiceEd2ElasticAggsPojo(chartResultPojoCopy, ValueTypeEnum.ARRAY_TYPE);
        //------同比结果，把开始结束时间置为同比的开始结束
        chartResultPojoCopy.setStartTime(lastYearStartTime);
        chartResultPojoCopy.setEndTime(lastYearEndTime);
        ElasticAggsPojo lastYearAggsPojo = invoiceEd2ElasticAggsPojo(chartResultPojoCopy, ValueTypeEnum.ARRAY_TYPE);
        //------处理环比、同比比例
        ElasticAggsPojo resultMom = systemChartHelp.getMomYoyResult(thisMonthAggsPojo, lastMonthAggsPojo);
        ElasticAggsPojo resultYoy = systemChartHelp.getMomYoyResult(thisMonthAggsPojo, lastYearAggsPojo);

        List<StatisticResultInfoDTO> statisticResultInfoDtoList = systemChartHelp.getInfoDtos4MomYoy(chartResultPojo, legendList, unitList, thisMonthAggsPojo, lastMonthAggsPojo, lastYearAggsPojo, resultMom, resultYoy);
        StatisticResultDTO statisticResultDto = new StatisticResultDTO(true, thisMonthAggsPojo.getTotal(), null, statisticResultInfoDtoList);
        return statisticResultDto;
    }

    /**
     * 获取发票统计结果
     * @param chartResultPojo 参数pojo
     * @param valueTypeEnum   返回值类型
     * @return com.xbongbong.paas.elasticsearch.pojo.ElasticAggsPojo
     * @throws XbbException
     * @author zcp
     * @date 2019/3/22 15:14
     * @since v1.0
     * @version v1.0
     */
    private ElasticAggsPojo invoiceEd2ElasticAggsPojo(ChartResultPojo chartResultPojo, ValueTypeEnum valueTypeEnum) throws XbbException {
        BoolQueryBuilder boolQueryBuilder = chartQueryHelp.invoiceQueryBuilder(chartResultPojo);
        String fieldTime = InvoiceEnum.getEsAttr4Keyword(InvoiceEnum.INVOICE_DATE);
        String sumField = InvoiceEnum.getEsAttr4Keyword(InvoiceEnum.TOTAL_MONEY);
        chartResultPojo.setValueTypeEnum(ValueTypeEnum.VALUE_MONEY);
        /*--------得到回款金额--------*/
        return systemChartHelp.getDouble4Agg(chartResultPojo, valueTypeEnum, IndexTypeEnum.IDX_SAAS_INVOICE, boolQueryBuilder, fieldTime, sumField);
    }
}
